' *** Documentation
' 
' VBScript NetworkHandler Class
' 
' last updated: Aug 2007
' author: klenwell@gmail.com
' 
' Class
' Methods
' 	Class_Initialize()
' 	pollNetwork
' 	isPingable(sIp)
' 	getNetworkIp
' 	setIpList(iSubStart, iSubEnd)
' 	PushArray(ByRef aRef, ByVal vIn)
' 
' NOTES
' 	Note the public properties listed at the beginning of the class
' 
' ____________________________________________________________________________*/


' *** CLASS
Class NetworkHandler

	' Properties
	Public iNumPings
	Public iPingTimeout
	Public NetworkIp
	Public NetworkDomain
	Public aIpList()
	Public aIpPing()
	Public aIpNoPing()
	Public aIpReport()
	Public aIpNoReport()
	
	Private oShell
	Private oFso
	
	' Constructor
	Private Sub Class_Initialize()
		iNumPings = 1
		iPingTimeout = 10
		Set oShell = CreateObject("WScript.Shell")
		Set oFso = CreateObject("Scripting.FileSystemObject") 
	End Sub
	
	' method: pollNetwork
	Public Sub pollNetwork
		For Each sIp in aIpList
			If isPingable(sIp) Then
				PushArray aIpPing, sIp
			Else
				PushArray aIpNoPing, sIp
			End If
		Next
	End Sub
		
	' method: isPingable
	Public Function isPingable(sIp)
	
		Dim OpenAsASCII: OpenAsASCII = 0
		Dim FailIfNotExist: FailIfNotExist = 0
		Dim ForReading: ForReading = 1
		
		' Create Objects
		Dim sTempFile: sTempFile = oFso.GetSpecialFolder(2).ShortPath & "\" & oFso.GetTempName
		oShell.Run "%comspec% /c ping.exe -n " & iNumPings & " -w " & iPingTimeout & " " & sIp & ">" & sTempFile, 0 , True
		Dim oFile: Set oFile = oFso.OpenTextFile(sTempFile, ForReading, FailIfNotExist, OpenAsASCII)
		
		' Result Tree
		Select Case InStr(oFile.ReadAll, "TTL=")
			Case 0 isPingable = False
			Case Else isPingable = True
		End Select
		
		' Close
		oFile.Close
		oFso.DeleteFile(sTempFile) 
	End Function
	
	' method: getNetworkIp
	Public Sub getNetworkIp
		Dim sTempFile: sTempFile = oFso.GetSpecialFolder(2) & "/ip.txt"

		' Fetch system data		
		If oShell.Environment("SYSTEM")("OS") = "" Then
			oShell.run "winipcfg /batch " & sTempFile, 0, True
		Else
			oShell.run "%comspec% /c ipconfig > " & sTempFile, 0, True
		End If
		
		' Parse file
		Dim sLine, sIp
		With oFso.GetFile(sTempFile).OpenAsTextStream
			Do While NOT .AtEndOfStream
				sLine = .ReadLine
				If InStr(sLine, "Address") <> 0 Then sIp = Mid(sLine, InStr(sLine, ":") + 2)
			Loop
			.Close
		End With
		
		' Trim string
		If sIp <> "" Then
			If Asc(Right(sIp, 1)) = 13 Then sIp = Left(sIp, Len(sIp) - 1)
		Else
			Err.Raise 107, "class: NetworkHandler.getNetworkIp", "ip not found"
		End If
		
		' Get Domain
		Dim aNetDomain: aNetDomain = Split(sIp, ".")
		
		NetworkDomain = aNetDomain(0) & "." & aNetDomain(1) & "." & aNetDomain(2) & "."
		NetworkIp = sIp
		oFso.GetFile(sTempFile).Delete
	End Sub
	
	' method: setIpList
	Public Sub setIpList(iSubStart, iSubEnd)
	
		If isNull(NetworkDomain) Then
			getNetworkIp
		End If
	
		Dim i, sIp
		For i = iSubStart To iSubEnd
			sIp = NetworkDomain & i
			pushArray aIpList, sIp
		Next
	End Sub
	
	' method: pushArray
	Public Sub PushArray(ByRef aRef, ByVal vIn)
		On Error Resume Next
		Dim iNew: iNew = Ubound(aRef) + 1
		If Err.Number <> 0 Then iNew = 0
		Redim Preserve aRef(iNew)
		aRef(iNew) = vIn		
	End Sub

End Class

' Test
If ( True ) Then
	MsgBox("begin NetworkHandler testing")

	Dim oTest: Set oTest = New NetworkHandler
	oTest.getNetworkIp
	MsgBox("NetworkIp / NetworkDomain : " & oTest.NetworkIp & "/" & oTest.NetworkDomain)

	oTest.setIpList 70, 80
	Dim ip, sIpDump
	For Each ip In oTest.aIpList
		sIpDump = sIpDump & "oTest.aIpList: " & ip & vbCrLF
	Next
	MsgBox(sIpDump)
	
	oTest.pollNetwork
	sIpDump = ""
	For Each ip In oTest.aIpPing
		sIpDump = sIpDump & "oTest.aIpPing: " & ip & vbCrLF
	Next
	MsgBox("Pinged Ips: " & VbCrLf & sIpDump)
End If


